package com.summer.safe.utils;

import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * 全局配置类
 *
 *
 */
@Configuration
public class Global {

    /**
     * 获取数据库类型
     */
    public static String dbName;
    /**
     * 当前对象实例
     */
    private static Global global = new Global();

    /**
     * 保存全局属性值
     */
    private static Map<String, String> map = Maps.newHashMap();


    /**
     * 显示/隐藏
     */
    public static final String SHOW = "0";
    public static final String HIDE = "1";

    /**
     * 是/否
     */
    public static final String YES = "0";
    public static final String NO = "1";


    /**
     * 成功
     */
    public static final String SUCCESS = "0";

    /**
     * 失败
     */
    public static final String FAIL = "1";

    /**
     * 对/错
     */
    public static final String TRUE = "true";
    public static final String FALSE = "false";


    /**
     * 所有上传文件物理路径
     */
    public static  String DIR_PATH;


    /**
     *   生产环境
     */
    public static final String PROFILES_PROD = "prod";

    /**
     *   开发环境
     */

    public static final String PROFILES_DEV = "dev";


    /**
     *   作用于判断
     */

    public static final String CODE_ZERO = "0";

    /**
     *   作用于判断
     */

    public static final String CODE_ONE = "1";


    /**
     *   操作类型(add)添加
     */

    public static final String OP_ADD = "add";


    /**
     *   操作类型(edit)修改
     */

    public static final String OP_EDIT = "edit";


    /**
     *   操作类型(view)查看
     */

    public static final String OP_VIEW = "view";

    /**
     *  用户数据
     */
    public static final  String USER_DATE ="USER";
    /**
     *  部门数据
     */
    public static final  String OFFICE_DATE ="OFFICE";


    /**
     * 验证码缓存
     */

    public static final String KAPTCHA_SESSION_KEY = "KAPTCHA_SESSION_KEY";

    /**
     * 验证码
     */
    public static final String CAPTCHA_CODE = "kaptcha";


    /**
     * 验证码key
     */
    public static final String CURRENT_CAPTCHA_KEY = "captchakey";
    /**
     * 验证码错误
     */
    public static final String CAPTCHA_ERROR = "captchaError";
    /**
     * 验证码开关
     */
    public static final String CURRENT_ENABLED = "CAPTCHAENABLED";

    /**
     * 字典数据
     */
    public static final String CACHE_DICT_MAP = "dictMap";
    /**
     * 字典类型
     */
    public static final String CACHE_DICT_TYPE_MAP = "dictTypeMap";

    /**
     * 用户缓存
     */
    public static final String USER_CACHE = "userCache";



    public static final String USER_SESSION_KEY = "userSessionKey";



    /**
     * 系统缓存
     */
    private static final String SYS_CACHE = "sysCache";
    /**
     * 用户的缓存id
     */
    public static final String USER_CACHE_ID_ = "id_";
    /**
     * 登录用户名
     */
    public static final String USER_CACHE_LOGIN_NAME_ = "ln";
    /**
     * office的id
     */
    public static final String USER_CACHE_LIST_BY_OFFICE_ID_ = "oid_";
    /**
     * 角色(暂未启用预留)
     */
    public static final String CACHE_ROLE_LIST = "roleList";
    /**
     * 按钮 (暂未启用预留)
     */
    public static final String CACHE_MENU_LIST = "menuList";
    /**
     * 当前用户区域(暂未启用预留)
     */
    public static final String CACHE_AREA_LIST = "areaList";
    /**
     * 所有的区域(暂未启用预留)
     */
    public static final String CACHE_AREA_ALL_LIST = "areaAllList";
    /**
     * 当前用户部门(暂未启用预留)
     */
    public static final String CACHE_OFFICE_LIST = "officeList";

    /**
     * 当前用户部门(暂未启用预留)
     */
    public static final String CACHE_OFFICE_LISTa = "officeLista";
    /**
     * 所有的部门(暂未启用预留)
     */
    public static final String CACHE_OFFICE_ALL_LIST = "officeAllList";

    /**
     * 上传文件虚拟路径
     */

    public static final String UPLOAD_FILES = "uploadfiles/";

    /**
     * 在线用户的缓存
     */
    public static final String Online_USERCACHE = "online-userCache";

    /**
     * 消息通知
     */
    public static final String DEFAULT_MESSAGE_PARAM = "message";



    /**
     * sessionid
     */
    public static final String SESSION_ID = "sessionid";

    /**
     * 踢出的缓存
     */


    public static final String KICK_OUT = "kickout";
    /**
     * 加盐参数
     */
    public static final String HASH_ALGORITHM = "SHA-1";
    /**
     * 循环次数
     */
    public final static int HASH_INTERATIONS = 1024;

    public static final int SALT_SIZE = 8;


    /**
     * 获取工程路径
     *
     * @return
     */
    public static String getProjectPath() {
        // 如果配置了工程路径，则直接返回，否则自动获取。
        String projectPath = "";//Global.getConfig("projectPath");
//        if (StringUtils.isNotBlank(projectPath)){
//            return projectPath;
//        }
        try {
            File file = new DefaultResourceLoader().getResource("").getFile();
            if (file != null) {
                while (true) {
                    File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
                    if (f == null || f.exists()) {
                        break;
                    }
                    if (file.getParentFile() != null) {
                        file = file.getParentFile();
                    } else {
                        break;
                    }
                }
                projectPath = file.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return projectPath;
    }

    /**
     * 获取当前对象实例
     */
    public static Global getInstance() {
        return global;
    }



    @Value("${pagehelper.helperDialect}")
    public void setName3(String dbName) {
        Global.dbName = dbName;
    }

    @Value("${config.file-upload-path}")
    public void setDirPath(String dirPath) {
        Global.DIR_PATH = dirPath;
    }

    /**
     * 获得项目路径
     */
    //当前项目下路径
    public static String getPath()  {
        String courseFile="";
        File directory = new File("");// 参数为空
        try {

             courseFile = directory.getCanonicalPath()+"\\summer-demo";
        }catch (IOException e) {
            e.printStackTrace();
        }

        return courseFile;
    }

    /**
     * 获取配置
     * @see ${fns:getConfig('adminPath')}
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (value == null){
           // value = loader.getProperty(key);
            map.put(key, value != null ? value : StringUtils.EMPTY);
        }
        return value;
    }

}
